import {toast} from '../../../components/GlobalToast';
import {listGroupByUserId} from '../services/eduService';
import {Actions} from 'react-native-router-flux';

const defaultState = {
	tab: 'group',
	groupListRefresh: false,
	groupList: [],
};

export default {

	namespace: 'home',
	
	state: {...defaultState},
	
	reducers: {
		
		changeTab(state, {payload}) {
			return {...state, ...{tab: payload}};
		},
		
		changeGroupListRefresh(state, {payload}) {
			return {...state, ...{groupListRefresh: payload}};
		},
		
		changeGroupList(state, {payload}) {
			return {...state, ...{groupList: payload}};
		},
		
	},
	
	effects: {
		
		*listGroupByUserId(action, {put}) {
			yield put({
				type: 'changeGroupListRefresh', 
				payload: true,
			});
			let res = yield listGroupByUserId();
			if(res.res == 1) {
				yield put({
					type: 'changeGroupList',
					payload: res.data,
				});
			} else {
				toast().show(res.msg);
			}
			yield put({
				type: 'changeGroupListRefresh', 
				payload: false,
			});
		},
		
		*toGroupChat(action, {put}) {
			Actions.push('groupChat');
		},
		
		*toGroupCreate(action, {put}) {
			Actions.push('groupCreate');
		},
		
		*toGroupJoin(action, {put}) {
			Actions.push('groupJoin');
		},
		
	},
	
}